from flask import Flask, request, jsonify
from flask_cors import CORS  # 导入 CORS
from model import AdvancedSupplyChainModel

app = Flask(__name__)
CORS(app)  # 启用 CORS 支持，允许所有域名访问

# 或者只允许特定域名（推荐）
# CORS(app, origins=["http://localhost:8080"])

model = AdvancedSupplyChainModel()

@app.route("/ping", methods=["GET"])
def ping():
    return "pong", 200

@app.route("/simulate", methods=["POST"])
def simulate():
    try:
        data = request.get_json()
        scenario_id = data.get("scenario_id")
        enterprise = data.get("enterprise")
        project_id = data.get("projectId")

        if not scenario_id or not enterprise:
            return jsonify({"code": 400, "msg": "Missing parameters"}), 400

        result = model.simulate_cascade(scenario_id, enterprise,project_id)
        return jsonify({"code": 200, "msg": "success", "data": result}), 200
    except ValueError as e:
        return jsonify({"code": 400, "msg": str(e)}), 400
    except Exception as e:
        return jsonify({"code": 500, "msg": f"Internal server error: {str(e)}"}), 500

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000, debug=True)